package com.example.rabbitmq.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Copyright(C), 2020-2021, AmbroseCdMeng
 * FileName:    RabbitmqConfig
 * Author:      AmbroseCdMeng
 * Date:        2021/2/27 14:52
 * Description: 配置队列
 * History:
 * <author>     <time>      <version>       <desc>
 * 作者姓名     修改时间        版本号           描述
 */
@Configuration
public class RabbitmqConfig {
    /**
     * 实现发送和接收队列
     */
    @Bean
    public Queue queue() {
        return new Queue("Queue1");
    }

    /**
     * 实现发送和接收对象
     */
    @Bean
    public Queue objectQueue() {
        return new Queue("object");
    }

    /**
     * 实现用接收器接收多个主题
     */
    @Bean
    public Queue queueMessage() {
        return new Queue("topic.a");
    }

    @Bean
    public Queue queueMessages() {
        return new Queue("topic.b");
    }

    @Bean
    TopicExchange exchange() {
        return new TopicExchange("topicExchange");
    }

    @Bean
    Binding bindingExchangeMessage(Queue queueMessage, TopicExchange exchange) {
        return BindingBuilder.bind(queueMessage).to(exchange).with("topic.a");
    }

    @Bean
    Binding bindingExchangeMessages(Queue queueMessage, TopicExchange exchange) {
        return BindingBuilder.bind(queueMessage).to(exchange).with("topic.#");
    }

    /**
     * 实现广播模式
     */
    @Bean
    public Queue queueA(){
        return new Queue("fanout.A");
    }

    @Bean
    public Queue queueB(){
        return new Queue("fanout.B");
    }

    @Bean
    FanoutExchange fanoutExchange(){
        return new FanoutExchange("fanoutExchange");
    }

    @Bean
    Binding bindingExchangeA(Queue queueA, FanoutExchange fanoutExchange){
        return BindingBuilder.bind(queueA).to(fanoutExchange);
    }

    @Bean
    Binding bindingExchange(Queue queueB, FanoutExchange fanoutExchange){
        return BindingBuilder.bind(queueB).to(fanoutExchange);
    }
}
